<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="filters">
				<el-form-item>
					<el-input v-model="filters.name" placeholder="姓名"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleAdd">新增</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="getShopPage()">刷新</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--列表-->
		<el-table :data="shops" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
			<el-table-column type="selection" width="55">
			</el-table-column>
			<el-table-column type="index" width="60">
			</el-table-column>
			<el-table-column prop="shopImg" label="店铺头像" width="130" sortable>
				<template slot-scope="scope">
					<el-image :src="$url+scope.row.shopImg"  max-width="110"></el-image>
				</template>
			</el-table-column>
			<el-table-column prop="shopName" label="店铺名" width="120" sortable>
			</el-table-column>
			<el-table-column prop="shopPhone" label="店铺电话" width="130" sortable>
			</el-table-column>
			<el-table-column prop="shopAddress" label="店铺地址" width="120" sortable>
			</el-table-column>
			<el-table-column prop="classifyList.length" label="分类数量" width="120" sortable>
			</el-table-column>
			<el-table-column prop="shopNotice" label="店铺公告" width="120" sortable>
			</el-table-column>
			<el-table-column prop="shippingFee" label="配送费" width="120" sortable>
			</el-table-column>
			<el-table-column prop="registerTime" label="注册时间" width="120" sortable>
			</el-table-column>



			<el-table-column label="操作" width="150">
				<template scope="scope">
					<el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
					<el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>

		<!--工具条-->
		<el-col :span="24" class="toolbar">
			<el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
			<el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :page-size="5" :total="total" style="float:right;">
			</el-pagination>
		</el-col>

		<!--编辑界面-->
		<el-dialog title="编辑" :visible.sync="editFormVisible" :close-on-click-modal="false">
			<el-form :model="editForm" label-width="80px" :rules="editFormRules" ref="editForm">
				<el-form-item label="店铺名" prop="shopName">
					<el-input v-model="editForm.shopName" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="店铺电话" prop="shopPhone">
					<el-input v-model="editForm.shopPhone" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="店铺头像" prop="shopPhone">
					<el-form-item label="图片上传" prop="goodsImg">
						<el-upload
								class="upload-demo"
								drag
								:data="{shopId:editForm.shopId}"
								ref="upload"
								:action="'/merchant/uploadShop'"
								multiple>
							<i class="el-icon-upload"></i>
							<div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
							<div class="el-upload__tip" slot="tip">只能上传jpg/png文件，且不超过500kb</div>
						</el-upload>
					</el-form-item>
				</el-form-item>
				<el-form-item label="店铺地址">
					<el-input type="textarea" v-model="editForm.shopAddress"></el-input>
				</el-form-item>
				<el-form-item label="店铺公告" prop="shopNotice">
					<el-input v-model="editForm.shopNotice" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="配送费/￥">
					<el-input-number v-model="editForm.shippingFee"></el-input-number>
				</el-form-item>
				<el-form-item label="配送时长/h">
					<el-input type="number" v-model="editForm.deliveryTime"></el-input>
				</el-form-item>
				<el-form-item label="营业时间">
					<el-input v-model="editForm.officeTime"></el-input>
				</el-form-item>
				<el-form-item label="下班时间">
					<el-input v-model="editForm.closingTime"></el-input>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="editFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button>
			</div>
		</el-dialog>

		<!--新增界面-->
		<el-dialog title="新增" :visible.sync="addFormVisible" :close-on-click-modal="false">
			<el-form :model="addForm" label-width="80px" :rules="addFormRules" ref="addForm">
				<el-form-item label="店铺名称">
					<el-input v-model="addForm.shopName"></el-input>
				</el-form-item>
				<el-form-item label="店铺电话">
					<el-input v-model="addForm.shopPhone"></el-input>
				</el-form-item>
				<el-form-item label="店铺地址">
					<el-input v-model="addForm.shopAddress"></el-input>
				</el-form-item>
				<el-form-item label="店铺公告">
					<el-input v-model="addForm.shopNotice"></el-input>
				</el-form-item>
				<el-form-item label="配送费">
					<el-input v-model="addForm.shippingFee"></el-input>
				</el-form-item>
				<el-form-item label="配送时长">
					<el-input type="number" v-model="addForm.deliveryTime"></el-input>
				</el-form-item>
				<el-form-item label="营业时间">
					<el-input type="time" v-model="addForm.officeTime"></el-input>
				</el-form-item>
				<el-form-item label="下班时间">
					<el-input type="time" v-model="addForm.closingTime"></el-input>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="addFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="addSubmit" :loading="addLoading">提交</el-button>
			</div>
		</el-dialog>
	</section>
</template>

<script>
	import util from '../../common/js/util'
	//import NProgress from 'nprogress'
	import { getUserListPage, removeUser, batchRemoveUser, editUser, addUser } from '../../api/api';

	export default {
		data() {
			return {
				filters: {
					name: ''
				},
				page: [],
				shops:[],
				total: 0,
				pageNum: 1,
				listLoading: false,
				sels: [],//列表选中列
				editFormVisible: false,//编辑界面是否显示
				editLoading: false,
				editFormRules: {
					name: [
						{ required: true, message: '请输入姓名', trigger: 'blur' }
					]
				},
				//编辑界面数据
				editForm: {
					
				},

				addFormVisible: false,//新增界面是否显示
				addLoading: false,
				addFormRules: {
					shopName: [
						{ required: true, message: '请输入姓名', trigger: 'blur' }
					],
					shopPhone: [
						{ required: true, message: '请输入电话', trigger: 'blur' }
					],
					shopAddress: [
						{ required: true, message: '请输入地址', trigger: 'blur' }
					],
					shippingFee: [
						{ required: true, message: '请设置配送费', trigger: 'blur' }
					],
					deliveryTime: [
						{ required: true, message: '请输入配送时长', trigger: 'blur' }
					],
					officeTime: [
						{ required: true, message: '请输入上班时间', trigger: 'blur' }
					],
					closingTime: [
						{ required: true, message: '请输入下班时间', trigger: 'blur' }
					],
					
				},
				//新增界面数据
				addForm: {
					shopName: '',
					shopPhone:'',
					shopAddress:'',
					shopNotice:'',
					shippingFee:'',
					deliveryTime:'',
					officeTime:'',
					closingTime:'',
					merchantId:this.$store.state.user.merchantId
				}

			}
		},
		methods: {
			//性别显示转换
			formatSex: function (row, column) {
				return row.sex == 1 ? '男' : row.sex == 0 ? '女' : '未知';
			},
			handleCurrentChange(val) {
				this.pageNum = val;
				this.getShopPage();
			},
			//获取用户列表
			getShopPage() {
				let para = {
					pageNum: this.pageNum,
					merchantId:this.$store.state.user.merchantId
				};
				this.listLoading = true;
				//NProgress.start();
				this.$axios({
					method:'get',
					params:para,
					url:'/merchant/getShopPage'
				}).then(r=>{
					if(r.data.err_code===0){
						this.page = r.data.result
						this.total = r.data.result.total
						this.pageNum = r.data.result.nextPage
						this.shops = this.page.list
						// this.shops.forEach(item=>{
						// 	item.registerTime=this.$dataFormat(item.registerTime)
						// })
					}
					this.listLoading=false
				}).catch(err=>{
					this.$message.error('网络异常，请稍后重试')
					this.listLoading=false
				})
			},
			//删除
			handleDel: function (index, row) {
				this.$confirm('确认删除该记录吗?', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					//NProgress.start();
					let para = { shopId: row.shopId };
					this.$axios({
						method:'get',
						params:para,
						url:'/merchant/delShop'
					}).then(r=>{
						if(r.data.err_code===0){
							this.$message.success('删除成功')
							this.getShopPage()
						}else{
							this.$message.error('网络错误，请稍后重试')
						}
						this.listLoading = false
					}).catch(err=>{
						this.listLoading = false
						this.$message.error('网络错误，请稍后重试')
					})
				}).catch(() => {

				});
			},
			//显示编辑界面
			handleEdit: function (index, row) {
				this.editFormVisible = true;
				this.editForm  = row
				// this.editForm = Object.assign({}, row);
			},
			//显示新增界面
			handleAdd: function () {
				this.addFormVisible = true;
			},
			//编辑
			editSubmit: function () {
				this.$refs.editForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
							this.editLoading = true;
							//NProgress.start();
							let para = Object.assign({}, this.editForm);
							this.$axios({
								method:'post',
								data:para,
								url:'/merchant/updShop'
							}).then(r=>{
								if(r.data.err_code===0){
									this.$message.success('修改成功')
									this.getShopPage()
								}else{
									this.$message.error('修改失败')
								}
								this.editFormVisible=false
								this.editLoading=false
							}).catch(err=>{
								this.$error()
								this.editFormVisible=false
								this.editLoading=false
							})
						});
					}
				});
			},
			//新增
			addSubmit: function () {
				this.$refs.addForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
							this.addLoading = true;
							//NProgress.start();
							let para = Object.assign({}, this.addForm);
							this.$axios({
								method:'post',
								data:para,
								url:'/merchant/regShop'
							}).then(r=>{
								if(r.data.err_code===0){
									this.$message.success('注册店铺成功!')
									for(let i in this.addForm){
										this.addForm[i]=''
									}
									this.addFormVisible = false;
									this.getShopPage()
								}else{
									this.$message.error('店铺名已存在！')
								}
								this.addLoading = false;
							}).catch(err=>{
								this.$error()
							})
						});
					}
				});
			},
			selsChange: function (sels) {
				this.sels = sels;
			},
			//批量删除
			batchRemove: function () {
				var ids = this.sels.map(item => item.id).toString();
				this.$confirm('确认删除选中记录吗？', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					//NProgress.start();
					let para = { ids: ids };
					batchRemoveUser(para).then((res) => {
						this.listLoading = false;
						//NProgress.done();
						this.$message({
							message: '删除成功',
							type: 'success'
						});
						this.getShopPage();
					});
				}).catch(() => {

				});
			}
		},
		created() {
			this.getShopPage();
		}
	}

</script>

<style scoped>

</style>